Skip to content

refactor(web): reduce cognitive complexity in middleware and monitors - #623

Merged
Starosdev merged 2 commits into
developfrom
fix/sonarqube-web
Jun 19, 2026
Merged

Starosdev merged 2 commits into
developfrom
fix/sonarqube-web

Conversation

@Starosdev

Copy link
Copy Markdown
Owner

Summary

Reduces SonarQube cognitive complexity (go:S3776) across the web middleware and background monitors. All flagged functions now sit at or below the allowed limit of 15.

Function File Before
AuthMiddleware middleware/auth.go 25
GetStatus missed_ping_monitor.go 21
checkAndSendHeartbeat heartbeat_monitor.go 19
LoggerMiddleware middleware/logger.go 17

Changes

Pure extraction of helpers; no behavior change.

  • AuthMiddleware — the returned handler is now authorize, with handleAuthDisabled and handleDocsPath for the two special path classes
  • LoggerMiddleware — cloneRequestBody, logRequestResult (status→level selection), logAPIBodies
  • GetStatus — resolveMissedPingSettings, countMonitoredDevices, buildNotifiedDeviceDetails, deviceNameAndWWN
  • checkAndSendHeartbeat — recordError/clearError (the repeated status-mutex blocks), countHealthyMonitored, sendHeartbeatNotification

Verification

  • go build ./webapp/backend/...
  • go vet on web + middleware packages
  • go test — middleware tests and the heartbeat/missed-ping monitor unit tests pass (verified against a throwaway InfluxDB)
  • gocognit -over 15 reports zero functions over the limit
  • gofmt clean

Notes

Continuation of SonarQube Group 5. Only the DB migration runner (Migrate, complexity 166) remains after this, to be handled in a dedicated change.

Extract focused helpers from the high-complexity middleware and monitor
functions. Behavior is unchanged; middleware and monitor unit tests pass.

- middleware/auth.go AuthMiddleware (25): authorize, handleAuthDisabled,
  handleDocsPath
- middleware/logger.go LoggerMiddleware (17): cloneRequestBody,
  logRequestResult, logAPIBodies
- missed_ping_monitor.go GetStatus (21): resolveMissedPingSettings,
  countMonitoredDevices, buildNotifiedDeviceDetails, deviceNameAndWWN
- heartbeat_monitor.go checkAndSendHeartbeat (19): recordError, clearError,
  countHealthyMonitored, sendHeartbeatNotification

All flagged functions now sit at or below the cognitive complexity
limit of 15.

Resolves SonarQube go:S3776 in web middleware and monitors.
- iterate devices by index in the new helpers (gocritic rangeValCopy,
  models.Device is large)
- nolint the relocated access-log Sprintf; %q would change the log
  quoting/escaping (gocritic sprintfQuotedString)
@Starosdev
Starosdev merged commit 8695dc8 into develop Jun 19, 2026
14 checks passed
@Starosdev
Starosdev deleted the fix/sonarqube-web branch June 19, 2026 11:45
Starosdev pushed a commit that referenced this pull request Jun 20, 2026
## [1.65.0-beta.1](v1.64.0...v1.65.0-beta.1) (2026-06-20)

### Features

* **api:** expose and update heartbeat_enabled on notify URLs ([99bb242](99bb242))
* **heartbeat:** filter notify URLs by heartbeat_enabled ([24f359a](24f359a))
* **notify:** add heartbeat_enabled field to notify_urls ([6432a2c](6432a2c))
* **notify:** LoadHeartbeatDatabaseUrls filters by heartbeat_enabled ([51d8662](51d8662))
* **thresholds:** detect Power-On Hours 16-bit counter rollover ([#595](#595)) ([#625](#625)) ([390de8b](390de8b))
* **ui:** add contextual help links for settings docs ([#492](#492)) ([8ceb12b](8ceb12b))
* **ui:** add contextual help links pointing to docs folder ([#492](#492)) ([7b484a0](7b484a0))
* **ui:** add heartbeat toggle per notification URL ([e6bbfea](e6bbfea))
* **ui:** add navigation visibility settings for ZFS, MDADM, Btrfs, Workload ([#588](#588)) ([fca1ae3](fca1ae3))
* **ui:** group MDADM and Workload pages by host ([#579](#579)) ([3e3aa73](3e3aa73))

### Bug Fixes

* **api:** return 404 when heartbeat toggle targets non-existent notify URL ([8c375c2](8c375c2))
* **backend:** pass SmartInfo by pointer to syncDeviceSelfTests (hugeParam) ([e027c33](e027c33))
* **backend:** satisfy gocritic hugeParam and govet fieldalignment lint ([be05416](be05416))
* **db:** simplify migration func to satisfy unlambda linter ([8158a34](8158a34))
* **ui:** style learn more link in Consumer Drive Profiles hint ([#492](#492)) ([ba5b6c3](ba5b6c3))

### Refactoring

* **collector:** reduce cognitive complexity in collector commands ([#617](#617)) ([dedf386](dedf386)), closes [#616](#616)
* **collector:** reduce cognitive complexity in device detection ([#619](#619)) ([b2e5a9d](b2e5a9d))
* **database:** reduce cognitive complexity in repository ([#621](#621)) ([4990d88](4990d88))
* **database:** reduce cognitive complexity in the migration runner ([#624](#624)) ([037c82d](037c82d))
* **frontend:** reduce cognitive complexity in components ([#620](#620)) ([278cd44](278cd44))
* **measurements:** reduce cognitive complexity in smart.go ([#616](#616)) ([b82e0f5](b82e0f5))
* **models:** reduce cognitive complexity in measurement models ([#622](#622)) ([900e4c3](900e4c3))
* **notify:** reduce cognitive complexity in notification senders ([#618](#618)) ([249d836](249d836)), closes [#616](#616) [#617](#617)
* **web:** reduce cognitive complexity in middleware and monitors ([#623](#623)) ([8695dc8](8695dc8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant